package algotithm.acwing.graphtheory.test859;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/16-21:01
 */
public class Main {

    private static int N = 100010;
    private static int M = 200010;
    private static int max = (int) 1e9;
    private static int[] p = new int[N];
    private static int n,m;
    static Comparator<Node> cmp = new Comparator<Node>() {
        @Override
        public int compare(Node o1, Node o2) {
            return o1.c - o2.c;
        }
    };

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

        String[] arr1 = reader.readLine().split(" ");
        n = Integer.parseInt(arr1[0]);
        m = Integer.parseInt(arr1[1]);
        Node[] edges = new Node[m];
        for (int i = 0; i < m; i++) {
            String[] arr = reader.readLine().split(" ");
            int a = Integer.parseInt(arr[0]);
            int b = Integer.parseInt(arr[1]);
            int c = Integer.parseInt(arr[2]);
            edges[i] = new Node(a,b,c);
        }

        Arrays.sort(edges,cmp);
        // 初始化
        for (int i = 1; i <= n; i++) {
            p[i] = i;
        }
        int cnt = 0;
        int res = 0;
        for (int i = 0; i < m; i++) {
            Node t = edges[i];
            int a1 = t.a;
            int b1 = t.b;
            int c1 = t.c;
            int fa = find(a1);
            int fb = find(b1);
            if (fa != fb) {
                p[fa] = fb;
                cnt++;
                res+=c1;
            }
            // 需要选中n-1条边构成树
            if (cnt == n - 1) {
                break;
            }
        }
        if (cnt == n - 1) {
            writer.write(res + "\n");
        } else {
            writer.write("impossible");
        }
        writer.flush();
        writer.close();
        reader.close();
    }

    private static class Node {
        private int a;
        private int b;
        private int c;
        public Node(int a,int b,int c) {
            super();
            this.a = a;
            this.b = b;
            this.c = c;
        }
    }

    private static int find(int x) {
        while (x != p[x]) {
            p[x] = p[p[x]];
            x = p[x];
        }
        return x;
    }


}
